let express=require("express");
let bodyParser=require("body-parser");
let db=require("./db");
let app=new express();
let url=require("url");

//将public文件作为静态目录
 app.use(express.static('public'));

let urlparser=bodyParser.urlencoded({extended:false});
app.get("/login",urlparser,function(req,res){
    res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
    // var urlstr=req.url;
    // const myURL=new URL("http:/"+urlstr);
    // var u=myURL.searchParams.get('username');
    // var p=myURL.searchParams.get('password');
    var u=req.query.username;
    var p=req.query.password;
    // console.log(u);
    // console.log(p);
    db.sql("select * from myuser where username='"+u+"'",function(err,data){
        if(err){
            res.end("数据库连接失败");
        }else{
            console.log("data",data);
            let a=data.recordset;
            let obj={
                code:400,
                msg:"用户名或密码错误"
            }
            if(a.length>0){
                if(p==a[0].password){
                    obj.code=200;
                    obj.msg="登录成功"
                    res.end(JSON.stringify(obj));
                }else{
                    res.end(JSON.stringify(obj));
                }
            }else{
                res.end(JSON.stringify(obj));
            }
        }
    })
    
})

app.get("/register",urlparser,function(req,res){
        // console.log(req.query.username);
        var u=req.query.username;
        var p=req.query.password;
    
        // var urlstr=req.url;
        // const myURL=new URL("http:/"+urlstr);
        // var u=myURL.searchParams.get('username');
        // var p=myURL.searchParams.get('password');
    
        db.sql("insert into myuser(username,password) values('"+u+"','"+p+"')",function(err,data){
            if(err){
                res.end("数据库连接失败")
            }else{
                console.log("data",data);
                let a = data.rowsAffected;
                let obj = {
                    code:400,
                    msg:"用户名或密码错误"
                }
                if(a.length > 0){
                    obj.code = 200;
                    obj.msg = "注册成功"
                    res.end(JSON.stringify(obj));
                }else{
                    res.end(JSON.stringify(obj));
                }
            }
        })
})
app.get("/delect",urlparser,function(req,res){
    var u=req.query.username;
    db.sql("delete from myuser where username='"+u+"'",function(err,data){
        if(err){
            res.end("数据库连接失败");
        }else{
            console.log("data",data);
            let a=data.rowsAffected;
            let obj={
                code:404,
                msg:"用户名不存在"
            }
            if(a){
                obj.code=200;
                obj.msg="删除成功"
                res.end(JSON.stringify(obj));
            }else{
                res.end(JSON.stringify(obj));
            }
        }
    })

})


app.listen(8080);
